package com.niiwoo.civet.mobile.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.niiwoo.civet.account.constant.AccountRegisterErrorCode;
import com.niiwoo.civet.account.dto.request.RebindCompanyBankCardRequestDTO;
import com.niiwoo.civet.account.dto.response.AccountCompanyDTO;
import com.niiwoo.civet.account.dto.response.RebindCompanyBankCardResponseDTO;
import com.niiwoo.civet.account.service.AccountCompanyDubboService;
import com.niiwoo.civet.account.service.bankcard.BankCardDubboService;
import com.niiwoo.civet.mobile.vo.request.bankcard.RebindCompanyBankCardReqVO;
import com.niiwoo.civet.mobile.vo.request.company.CompanyInfoModifyRequestVO;
import com.niiwoo.civet.mobile.vo.request.company.TradePwdModifyRequestVO;
import com.niiwoo.civet.mobile.vo.response.FormResponseVO;
import com.niiwoo.civet.mobile.vo.response.bankcard.RebindCompanyBankCardResponseVO;
import com.niiwoo.tripod.base.enums.UserTypeEnum;
import com.niiwoo.tripod.lanmao.component.LanMaoGatewayService;
import com.niiwoo.tripod.lanmao.component.LanMaoSequence;
import com.niiwoo.tripod.lanmao.enums.SkipWayEnum;
import com.niiwoo.tripod.lanmao.request.ChangeCompanyInformationRequest;
import com.niiwoo.tripod.lanmao.request.ChangeTradePasswordRequest;
import com.niiwoo.tripod.lanmao.response.LanMaoGatewayDTO;
import com.niiwoo.tripod.provider.exception.BizException;
import com.niiwoo.tripod.web.util.PrincipalContext;
import com.niiwoo.tripod.web.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Date;

/**
 * 企业账户相关
 *
 * @author: liaozuliang
 * @date: 2018/8/30
 */
@Slf4j
@RestController
@RequestMapping("/companyAccount")
@Api(tags = "企业账户相关", description = "/companyAccount")
public class CompanyAccountController {

    @Reference(version = "1.0.0")
    private AccountCompanyDubboService accountCompanyDubboService;

    @Autowired
    private LanMaoGatewayService lanMaoGatewayService;

    @Autowired
    private LanMaoSequence lanMaoSequence;

    @Reference(version = "1.0.0")
    private BankCardDubboService bankCardDubboService;

    @PostMapping("/modifyTradePwd")
    @ApiOperation("修改交易密码")
    public Result<FormResponseVO> modifyTradePwd(@RequestBody @Valid TradePwdModifyRequestVO requestVO) {
        if (!UserTypeEnum.ORGANIZATION.equals(PrincipalContext.getUserPrincipal().getUserType())) {
            throw new BizException("ACC17008");
        }

        AccountCompanyDTO accountCompanyDTO = accountCompanyDubboService.queryAccountCompanyByUserId(PrincipalContext.getUserId());
        if (accountCompanyDTO == null) {
            throw new BizException(AccountRegisterErrorCode.ACCOUNT_NOT_EXISTS);
        }

        ChangeTradePasswordRequest request = new ChangeTradePasswordRequest();
        request.setRequestNo(lanMaoSequence.getRequestNo());
        request.setPlatformUserNo(accountCompanyDTO.getAccountNo());
        request.setTimestamp(new Date());
        request.setNiiwooRedirectUrl(requestVO.getRedirectUrl());
        request.setIsSkip(SkipWayEnum.enumOf(requestVO.getSkip()));

        LanMaoGatewayDTO gatewayDTO = lanMaoGatewayService.changeTradePassword(request);

        FormResponseVO formResponseVO = new FormResponseVO();
        formResponseVO.setAction(gatewayDTO.getAction());
        formResponseVO.setParams(gatewayDTO.getParams());

        return Result.with(formResponseVO);
    }

    @PostMapping("/modifyCompanyInfo")
    @ApiOperation("修改企业资料")
    public Result<FormResponseVO> modifyCompanyInfo(@RequestBody @Valid CompanyInfoModifyRequestVO requestVO) {
        if (!UserTypeEnum.ORGANIZATION.equals(PrincipalContext.getUserPrincipal().getUserType())) {
            throw new BizException("ACC17008");
        }

        AccountCompanyDTO accountCompanyDTO = accountCompanyDubboService.queryAccountCompanyByUserId(PrincipalContext.getUserId());
        if (accountCompanyDTO == null) {
            throw new BizException(AccountRegisterErrorCode.ACCOUNT_NOT_EXISTS);
        }

        ChangeCompanyInformationRequest request = new ChangeCompanyInformationRequest();
        request.setRequestNo(lanMaoSequence.getRequestNo());
        request.setPlatformUserNo(accountCompanyDTO.getAccountNo());
        request.setNiiwooRedirectUrl(requestVO.getRedirectUrl());
        request.setTimestamp(new Date());

        LanMaoGatewayDTO gatewayDTO = lanMaoGatewayService.changeCompanyInformation(request);

        FormResponseVO formResponseVO = new FormResponseVO();
        formResponseVO.setAction(gatewayDTO.getAction());
        formResponseVO.setParams(gatewayDTO.getParams());

        return Result.with(formResponseVO);
    }

    @PostMapping("/rebindCompanyBankCard")
    @ApiOperation(value = "企业换绑银行卡")
    public Result<RebindCompanyBankCardResponseVO> rebindCompanyBankCard(@RequestBody @Valid RebindCompanyBankCardReqVO rebindCompanyBankCardReqVO) {
        String orgId = PrincipalContext.getUserId();
        log.info("企业换绑银行卡,机构ID={},请求参数={}", orgId, JSON.toJSONString(rebindCompanyBankCardReqVO));
        AccountCompanyDTO accountCompanyRespDTO = accountCompanyDubboService.getInvestOrgByOrgId(orgId);
        RebindCompanyBankCardRequestDTO rebindCompanyBankCardRequestDTO = new RebindCompanyBankCardRequestDTO();
        rebindCompanyBankCardRequestDTO.setPlatformUserNo(accountCompanyRespDTO.getAccountNo());
        rebindCompanyBankCardRequestDTO.setBankCardNo(rebindCompanyBankCardReqVO.getBankCardNo());
        rebindCompanyBankCardRequestDTO.setBankCode(rebindCompanyBankCardReqVO.getBankCode());
        rebindCompanyBankCardRequestDTO.setMobileNo(accountCompanyRespDTO.getContactMobileNo());
        rebindCompanyBankCardRequestDTO.setOrgId(orgId);
        rebindCompanyBankCardRequestDTO.setRedirectURL(rebindCompanyBankCardReqVO.getRedirectUrl());

        RebindCompanyBankCardResponseVO rebindCompanyBankCardResponseVO = new RebindCompanyBankCardResponseVO();
        RebindCompanyBankCardResponseDTO rebindCompanyBankCardResponseDTO = bankCardDubboService.rebindCompanyBankCard(rebindCompanyBankCardRequestDTO);
        rebindCompanyBankCardResponseVO.setAction(rebindCompanyBankCardResponseDTO.getAction());
        rebindCompanyBankCardResponseVO.setParams(rebindCompanyBankCardResponseDTO.getParams());

        log.info("企业换绑银行卡,机构ID={},请求参数={}", orgId, JSON.toJSONString(rebindCompanyBankCardResponseVO));
        return Result.with(rebindCompanyBankCardResponseVO);
    }
}
